const http = require("http");
const fs = require("fs");
const path = require("path");

var data = path.join(__dirname,"/data.json");
var dataObj = fs.readFileSync(data).toString("utf-8");
dataObj = JSON.parse(dataObj)
var userList = dataObj["userList"]
console.log(userList)

http.createServer((req, res) => {
    var str = ""
    req.on("data",(chunk)=>{
        str += chunk
    })
    req.on("end",()=>{
        var obj = JSON.parse(str);
        // console.log(obj);
        var flag = true;
        for(var i=0;i<userList.length;i++){
            if(userList[i].username == obj.username && userList[i].password == obj.password){
                console.log("登录成功")
                flag = false;
            }
        }
        if(flag == true){
            console.log("用户名、密码不对");
        }
    })
    
}).listen(8081);
console.log("server is listening 8081");

/**
 * 框架，KOA EXpress titbit
 */